package com.guessdroid.UI;


import com.guessdroid.R;

import com.guessdroid.labeling.GContext;
import com.guessdroid.labeling.GContextList;
import com.guessdroid.util.AndroidWriter;
import com.guessdroid.util.ExceptionHandler;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class HomeActivity extends Activity {
	private GContextList list;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler());
		setContentView(R.layout.activity_home);
		list = GContextList.read();
		initializeContextListView(list);
		setValueForTextView(R.id.successPercent, list.successPercent() + "%");
		setButtonClickListeners();
	}

	private void initializeContextListView(GContextList list) {
		ListView contextList = (ListView) findViewById(R.id.contextList);
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		GContextAdapter adapter = new GContextAdapter(this, R.layout.list_item,
				list.contexts, inflater);
		contextList.setAdapter(adapter);

		contextList.setClickable(true);
		contextList.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				moveToLearnSelectedContext(position);
			}
		});
	}

	private void moveToLearnSelectedContext(int position) {
		ListView contextList = (ListView) findViewById(R.id.contextList);
		GContext selectedContext = (GContext) contextList
				.getItemAtPosition(position);
		Intent intent = new Intent(HomeActivity.this, LearnActivity.class);
		intent.putExtra("selected_context", selectedContext);
		startActivity(intent);
	}

	private void setButtonClickListeners() {
		Button newContext = (Button) findViewById(R.id.newContextButton);
		newContext.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this,
						NewContextActivity.class);
				startActivity(intent);
			}
		});
		Button guess = (Button) findViewById(R.id.guessButton);
		guess.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(HomeActivity.this,
						GuessActivity.class);
				intent.putExtra("contexts", list);
				startActivity(intent);
			}
		});
		Button reset = (Button) findViewById(R.id.resetButton);
		reset.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				AndroidWriter.deleteData();
				finish();
				startActivity(getIntent());
			}
		});
	}

	private void setValueForTextView(int id, String value) {
		TextView element = (TextView) findViewById(id);
		if (element != null) {
			element.setText(value);
		}
	}

}
